package com.slfx.data.permission.controller;

import com.slfx.data.permission.dto.DeptTreeDTO;
import com.slfx.data.permission.entity.SystemDept;
import com.slfx.data.permission.service.SystemDeptService;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toList;

/**
 * @Author: slfx
 * @Date: 2022-12-31
 * @Description:
 **/
@RestController
@RequestMapping("/systemDept")
public class SystemDeptController {

    @Resource
    private SystemDeptService systemDeptService;

    /**
     * 根据ID获取
     *
     * @param id
     * @return
     */
    @GetMapping("/getById")
    public SystemDept getById(@RequestParam("id") Long id) {
        SystemDept asset = systemDeptService.getById(id);
        return asset;
    }

    /**
     * 新增
     *
     * @param dept
     * @return
     */
    @PostMapping("/save")
    public Long save(@RequestBody SystemDept dept) {
        systemDeptService.save(dept);
        return dept.getId();
    }

    /**
     * 根据父部门获取到子部门
     *
     * @param parentId
     * @return
     */
    @GetMapping("/getChildrenByParentId")
    public List<SystemDept> getChildrenByParentId(@RequestParam("parentId") Long parentId) {
        List<SystemDept> list = systemDeptService.getChildrenByParentId(parentId);
        return list;
    }


    /**
     * 数据来源：获取部门树
     *
     * @return
     */
    @PostMapping("/getDeptTree")
    public List<DeptTreeDTO> getDeptTree() {
        //获取到所有
        List<SystemDept> list = systemDeptService.list(null);
        //转换
        List<DeptTreeDTO> convertList = new ArrayList<>();
        for (SystemDept dept : list) {
            DeptTreeDTO deptTreeDTO = new DeptTreeDTO();
            BeanUtils.copyProperties(dept, deptTreeDTO);
            convertList.add(deptTreeDTO);
        }
        ;
        //构建树形返回
        List<DeptTreeDTO> tree = this.populateTree(convertList);
        return tree;
    }

    /**
     * 将部门组装成树形
     *
     * @param depts
     * @return
     */
    public List<DeptTreeDTO> populateTree(List<DeptTreeDTO> depts) {
        if (CollectionUtils.isEmpty(depts)) {
            return Collections.emptyList();
        }
        Map<Long, List<DeptTreeDTO>> treeMap = depts.stream().filter(data -> data != null)
                .collect(groupingBy(a -> a.getParentId()));
        List<DeptTreeDTO> parentRegions = treeMap.get(0L);
        if (CollectionUtils.isEmpty(parentRegions)) {
            return Collections.emptyList();
        }
        depts.stream().filter(data -> data != null).forEach(a -> {
            List<DeptTreeDTO> regionList = treeMap.get(a.getId());
            if (!CollectionUtils.isEmpty(regionList)) {
                regionList = regionList.stream().filter(Objects::nonNull)
                        .sorted(Comparator.comparing(DeptTreeDTO::getId)).collect(toList());
            }
            a.setChildren(regionList);
        });
        return parentRegions;
    }


}
